package com.cmdi.opensite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmdi.opensite.entity.User;
import com.cmdi.opensite.mapper.UserMapper;
import com.cmdi.opensite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }
    
    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }
    
    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (findByUsername(user.getUsername()) != null) {
            return false;
        }
        // 检查邮箱是否已存在
        if (findByEmail(user.getEmail()) != null) {
            return false;
        }
        // 密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        // 设置默认角色和状态
        user.setStatus(1); // 1表示启用
        return save(user);
    }
    
    @Override
    public User login(String username, String password) {
        User user = findByUsername(username);
        if (user != null && passwordEncoder.matches(password, user.getPassword())) {
            return user;
        }
        return null;
    }
    
    @Override
    public boolean updateUserInfo(User user) {
        // 更新用户信息，但不更新密码
        User existingUser = getById(user.getId());
        if (existingUser != null) {
            existingUser.setNickname(user.getNickname());
            existingUser.setEmail(user.getEmail());
            existingUser.setPhone(user.getPhone());
            existingUser.setAvatar(user.getAvatar());
            return updateById(existingUser);
        }
        return false;
    }
    
    @Override
    public boolean changePassword(Long userId, String oldPassword, String newPassword) {
        User user = getById(userId);
        if (user != null && passwordEncoder.matches(oldPassword, user.getPassword())) {
            user.setPassword(passwordEncoder.encode(newPassword));
            return updateById(user);
        }
        return false;
    }
    
    @Override
    public IPage<User> selectUserPage(IPage<User> page, String keyword) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like("username", keyword).or().like("nickname", keyword).or().like("email", keyword);
        }
        return page(page, queryWrapper);
    }
    
    @Override
    public List<String> getUserRoles(Long userId) {
        // 这里应该从用户角色关联表查询，暂时返回默认角色
        List<String> roles = new ArrayList<>();
        roles.add("ROLE_USER");
        return roles;
    }
    
    @Override
    public boolean updateUserRoles(Long userId, List<Long> roleIds) {
        // 这里应该更新用户角色关联表，暂时返回true
        return true;
    }
}